package com.example.sunmiv1.utils


import android.util.Log
import com.example.sunmiv1.BuildConfig
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken


object GsonTools {
    private val TAG=javaClass.simpleName
    fun <T> jsonToBean(clazz: Class<T>,body:String):T?{
        return try {
            val gson= Gson()
            gson.fromJson<T>(body,clazz).also {
                //Log.e(TAG, "jsonToBean: sucess $it" )
            }
        }catch (e:Exception){
            Log.e(TAG, "jsonToBean: error $clazz==$e" )
            return null
        }
    }

    fun <T> jsonToBeanFromat(clazz: Class<T>,body:String):T?{
        return try {
            val gson= Gson()
            gson.fromJson<T>(body,clazz).also {
                //Log.e(TAG, "jsonToBean: sucess $it" )
            }
        }catch (e:Exception){
            Log.e(TAG, "jsonToBean: error $clazz==$e" )
            return null
        }
    }

    fun <T> jsonHtmlToBean(clazz: Class<T>,body:String):T?{
        return try {
            val gson= Gson()
            gson.fromJson<T>(body,clazz).also {
                //Log.e(TAG, "jsonToBean: sucess $it" )
            }
        }catch (e:Exception){
            Log.e(TAG, "jsonToBean: error $clazz==$e" )
            return null
        }
    }

    fun <T> beanToJson(body:T?,debug:Boolean=true):String{
        if(BuildConfig.DEBUG || debug){
            if(body==null){
                return ""
            }
            return try {
                GsonBuilder().setPrettyPrinting().create().toJson(body)
                //Gson().toJson(body)
            }catch (e:Exception){
                Log.e(TAG, "beanToJson: error $e" )
                ""
            }
        }else{

        }
        return ""
    }

    fun <T> beanToJsonFromat(body:T?):String{
        if(body==null){
            return ""
        }
        return try {
            Gson().toJson(body)
        }catch (e:Exception){
            ""
        }
    }

    fun jsonToList(json:String):List<String>{
        return try {
            val type=object : TypeToken<List<String>>(){}.type
            Gson().fromJson<List<String>>(json,type)?: arrayListOf()
        }catch (e:java.lang.Exception){
            Log.e(TAG, "jsonToList: error $e", )
            arrayListOf<String>()
        }
    }

    fun <T> jsonToClassList(json:String):List<T>{
        Log.e(TAG, "jsonToClassList: $json" )
        if(json.isBlank()){
            return arrayListOf()
        }
        return try {
            val type=object : TypeToken<List<T>>(){}.type
            Gson().fromJson<List<T>>(json,type)?: arrayListOf()
        }catch (e:java.lang.Exception){
            Log.e(TAG, "jsonToClassList: error $e" )
            arrayListOf<T>()
        }
    }

    fun <T> getObjectList(jsonString: String?, cls: Class<T>?): List<T> {
        val list: MutableList<T> = ArrayList()
        try {
            val gson = Gson()
            val arry = JsonParser().parse(jsonString).asJsonArray
            for (jsonElement in arry) {
                list.add(gson.fromJson(jsonElement, cls))
            }
        } catch (e: java.lang.Exception) {
            Log.e(TAG, "getObjectList: error $e" )
            e.printStackTrace()
        }
        return list
    }
}